<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class UserFull extends CFormModel{

    public $id;
    public $name;
    public $password;
    public $passwordConfirm;
    public $email;
    public $nick;
    public $isAdmin;
    public $space;
    public $usedSpace;    public $fileCount;
    public $deviceCount;

    public $avatar;
    public $status;
    public $realName;
    public $phone;
    public $qq;
    public $website;
    public $memo;

    public $oldPassword;
    public $oldEmail;
    public $oldNick;
    public $oldIsAdmin;
    public $oldSpace;
    public $oldRealName;
    public $oldPhone;
    public $oldQq;
    public $oldWebsite;
    public $oldMemo;

    public $statusArray = array("1" => '启用',"0" => '冻结');
    
    public $adminArray = array("0"=>"用户","1"=>"管理员");

    public function init(){
        $this->isAdmin="0";
        $this->space = Yii::app()->params["app"]["defaultSpace"];    }
    
    public function validateEmail(){
        if(strlen($this->email)>0){
            $validator = new CEmailValidator;
            if($validator->validateValue($this->email)==false){
                $this->addError("email","电子邮件格式不正确");
                return false;
            }
        }
        return true;
    }
    public function rules()
    {
        return array(
        array('nick', 'length', 'max'=>255),
        array('email', 'length', 'max'=>255),
        array('password', 'length', 'max'=>20),
        array('passwordConfirm', 'length', 'max'=>20),
        array('space', 'numerical'),
        array('isAdmin', 'numerical', 'integerOnly'=>true),
        array('passwordConfirm', 'compare', 'compareAttribute' => 'password'),
        array('qq', 'length', 'max'=>255),
        array('website', 'length', 'max'=>255),
        array('realName', 'length', 'max'=>255),
        array('memo', 'length', 'max'=>255),
        array('status', 'length', 'max'=>255),
        array('phone', 'numerical'),
        array('password', 'checkPassword'),
        );
    }
    public function attributeLabels()
    {
        return array(
        	'name'             => '用户名',
         	'fileCount'        => '文件',
            'nick'             => '昵称',
            'password'         => '用户密码',
            'passwordConfirm' => '确认密码',
            'realName'         => '真实姓名',
            'email'            => '电子邮件',
            'space'            =>'可用空间',
            'isAdmin'          => '赋予管理员权限',
        	'disabled'      => '用户状态',
            'phone'            => '手机',
            'qq'               => 'QQ号码',
            'website'          => '网站',
            'memo'             => '更多备注',
			'deviceCount'      => '激活设备数',
			'status'      => '用户状态',
        );
    }

    
    public function checkPassword(){
        if(!$this->hasErrors())
        {
            if (!empty($this->password)){
                $strArr = explode(" ", $this->password);
                if(count($strArr)!=1){
                    $this->addError('password','密码中不能有空格');
                    return;
                }

                if (strlen($this->password) < 5){
                    $this->addError('password','密码必须大于或等于5位');
                    return;
                }
            }
        }
    }


    
    private function _fillUser($userData){

        $this->name       = $userData->user_name;
        $this->status   = $userData->user_status;
        $this->oldPassword = $userData->user_pass;        if(!isset($this->name)){
            $this->name="";
        }
        if(!isset($this->status)){
            $this->status=0;
        }
    }
    
    public function fillFileInfo($userId,$startDate,$endDate){
        if(isset($userId)){
            $this->fileCount = UserFile::model()->count("user_id=? and created_at>='".$startDate." 00:00:00' and created_at<='".$endDate." 23:59:59'",array($userId));        }else{
            $this->fileCount = 0;
        }
    }
    
    public function fillDeviceInfo($userId,$startDate,$endDate){
        if(isset($userId)){
            $this->deviceCount = UserDevice::model()->count("user_id=? and created_at>='".$startDate." 00:00:00' and created_at<='".$endDate." 23:59:59'",array($userId));        }else{
            $this->deviceCount = 0;
        }
    }
    
    private function _fillUserMeta($userMetaData){
        foreach($userMetaData as $index=>$userMetaItem){
            $metaKey = $userMetaItem->meta_key;
            $metaValue = $userMetaItem->meta_value;
            if($metaKey=="avatar"){
                $this->avatar=$metaValue;
            }
            if($metaKey=="email"){
                $this->email=$metaValue;
                $this->oldEmail=$this->email;
            }
            if($metaKey=="nick"){
                $this->nick=$metaValue;
                $this->oldNick=$this->nick;
            }
            if($metaKey=="phone"){
                $this->phone=$metaValue;
                $this->oldPhone=$this->phone;
            }
            if($metaKey=="space"){
                $this->space=(int)$metaValue;
                $this->oldSpace=$this->space;
            }
            if($metaKey=="is_admin"){
                $this->isAdmin=$metaValue;
                $this->oldIsAdmin=$this->isAdmin;
            }
            if($metaKey=="realname"){
                $this->realName=$metaValue;
                $this->oldRealName=$this->realName;
            }
            if($metaKey=="qq"){
                $this->qq=$metaValue;
                $this->oldQq=$this->qq;
            }
            if($metaKey=="website"){
                $this->website=$metaValue;
                $this->oldWebsite=$this->website;
            }
            if($metaKey=="memo"){
                $this->memo=$metaValue;
                $this->oldMemo=$this->memo;
            }
        }
        if(!isset($this->avatar)){
            $this->avatar=Yii::app()->params["defaultAvatar"];
        }
        if(!isset($this->email)){
            $this->email="";
            $this->oldEmail="";
        }
        if(!isset($this->phone)){
            $this->phone="";
            $this->oldPhone="";
        }
        if(!isset($this->isAdmin)){
            $this->isAdmin=false;
            $this->oldIsAdmin=false;
        }
        if(!isset($this->realName)){
            $this->realName="";
            $this->oldRealName="";
        }
        if(!isset($this->qq)){
            $this->qq="";
            $this->oldQq="";
        }
        if(!isset($this->website)){
            $this->website="";
            $this->oldWebsite="";
        }
        if(!isset($this->memo)){
            $this->memo="";
            $this->oldMemo="";
        }
        if(!isset($this->space)){
            $this->space=Yii::app()->params["app"]["defaultSpace"];
            $this->oldSpace=Yii::app()->params["app"]["defaultSpace"];
        }
    }
    
    public function initData($id)
    {
        $this->id = $id;
        $model=User::model()->findByPk($id);
        if(!isset($model))
        throw new CHttpException(404,'The requested page does not exist.');
        $this->_fillUser($model);
        $metaModel = UserMeta::model()->findAll(array('condition'=>"user_id=$id"));
        $this->_fillUserMeta($metaModel);
    }
    
    public function statusDetail(){
        if($this->status==0){
            return "<img src='".Yii::app()->request->baseUrl.Yii::app()->params["iconUserDisabled"]."' style='width:24px'>";
        }else{
            return "<img src='".Yii::app()->request->baseUrl.Yii::app()->params["iconUserEnabled"]."' style='width:24px'>";
        }
    }
}